package com.link.base.base.alert.controller;

import com.link.base.base.alert.model.Alert;
import com.link.base.base.alert.service.AlertService;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.KeyGenerateService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 预警模块-头
 *
 * @author 宁蓝昊
 * @date 2019/09/28 16:03
 */
@Controller
@RequestMapping("/link/alert")
public class AlertController extends BasicController<Alert> {
    private static final Logger logger = LogManager.getLogger(AlertController.class);
    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private AlertService alertService;

    @Override
    public BasicService<Alert> getBasicService() throws Exception {
        return alertService;
    }


    @RequestMapping({"/saveAlert"})
    @ResponseBody
    public Map<String, Object> saveAlert(@RequestBody Alert entity, HttpSession session,
                                         HttpServletRequest request, HttpServletResponse response) {
        HashMap result = new HashMap(4);
        try {
            alertService.saveAlert(entity);
            result.put("newRow", null);
            result.put("success", true);
        } catch (BasicServiceException var7) {
            logger.error("BasicController捕获异常: ", var7);
            result.put("success", false);
            result.put("result", var7.getMessage());
            result.put("detailMessage", var7.getDetailMessage());
        } catch (Exception var8) {
            logger.error("BasicController捕获异常: ", var8);
            result.put("success", false);
            result.put("result", var8.getMessage());
        }
        return result;
    }

    @RequestMapping({"/executeAlert"})
    @ResponseBody
    public Map<String, Object> executeAlert(@RequestBody Alert entity, HttpSession session,
                                         HttpServletRequest request, HttpServletResponse response) {
        HashMap result = new HashMap(4);
        try {
            alertService.executeAlert();
            result.put("newRow", null);
            result.put("success", true);
        } catch (BasicServiceException var7) {
            logger.error("BasicController捕获异常: ", var7);
            result.put("success", false);
            result.put("result", var7.getMessage());
            result.put("detailMessage", var7.getDetailMessage());
        } catch (Exception var8) {
            logger.error("BasicController捕获异常: ", var8);
            result.put("success", false);
            result.put("result", var8.getMessage());
        }
        return result;
    }
}
